package com.ccc.controller;


import com.ccc.entity.StuGradeDis;
import com.ccc.entity.StuReport;
import com.ccc.mapper.StuReportMapper;
import com.ccc.utils.JwtUtil;
import com.ccc.utils.ResponseResult;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ccc
 * @since 2022-01-03
 */
@Controller
public class StuReportController {
    @Autowired
    StuReportMapper stuReportMapper;

    /**
     * 教师获取学生的实习报告，返回的是所有学生的实习报告
     *
     * @return
     */
    @GetMapping("/teacher/reportList")
    @PreAuthorize("hasAuthority('teacher')")
    @ResponseBody
    public ResponseResult reportList() {
        return new ResponseResult(200, "success", stuReportMapper.reportList());
    }

    /**
     * 学生获取实习报告，返回的是请求学生的实习报告
     *
     * @param request
     * @return
     */
    @GetMapping("/stu/reportList")
    @PreAuthorize("hasAuthority('student')")
    @ResponseBody
    public ResponseResult stuReportList(HttpServletRequest request) {
        String token = request.getHeader("token");
        String username;

        try {
            Claims claims = JwtUtil.parseJWT(token);
            username = claims.getSubject();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(400, "token Error!");
        }

        return new ResponseResult(200, "success", stuReportMapper.stuReportList(username));
    }

    /**
     * 学生添加日志
     *
     * @param stuReport
     * @param request
     * @return
     */
    @PostMapping("/stu/addReport")
    @PreAuthorize("hasAuthority('student')")
    @ResponseBody
    public ResponseResult addReport(@RequestBody StuReport stuReport, HttpServletRequest request) {
        String token = request.getHeader("token");
        String username;
        try {
            Claims claims = JwtUtil.parseJWT(token);
            username = claims.getSubject();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(400, "token Error!");
        }
        stuReportMapper.addReport(username, stuReport.getTitle(), stuReport.getContent());
        return new ResponseResult(200, "success");
    }

    /**
     * 教师请求学生成绩的修改，
     * @param stuReport
     * @return
     */
    @PostMapping("/teacher/updateGrade")
    @PreAuthorize("hasAuthority('teacher')")
    @ResponseBody
    public ResponseResult updateGradeById(@RequestBody StuReport stuReport) {
        stuReportMapper.updateScoreById(stuReport.getReportId(), stuReport.getGrade());
        return new ResponseResult(200, "success");
    }

    /**
     * 获取每个成绩的人数 例如 优：10人 中：15人。。。
     *
     * @return
     */
    @GetMapping("/teacher/getGradeDis")
    @PreAuthorize("hasAuthority('teacher')")
    @ResponseBody
    public ResponseResult getGradeDis() {

        List<StuGradeDis> gradeDis = this.stuReportMapper.getGradeDis();

//        System.out.println(gradeDis);

        return new ResponseResult(200, "success", gradeDis);
    }
}
